var queryConditions = {}, url = ctx + "/jump/", gridQryUrl = url + "queryAllDictFacility",
    CommunityFacility = {
        init: function () {
            this.initDom();
            this.loadGrid();
            this.addEvent()
        },
        initDom: function () {
            $.jgrid.defaults.styleUI = "Bootstrap";
        },
        loadGrid: function () {

            function operate(cellvalue, options, rowObject) {
                var html = "";
                //html += '<a href="javascript:;" class="btn btn-info btn-xs" onclick="StaticTypeHandle.view(\'' + rowObject.id + '\')"><i class="fa fa-search-plus"></i> 查看</a>';
                html += '  <a href="javascript:;" class="btn btn-success btn-xs" onclick="CommunityFacilityHandle.update(\'' + rowObject.id + '\')"><i class="fa fa-edit"></i> 修改</a>';
                html += '  <a href="javascript:;" class="btn btn-danger btn-xs" onclick="CommunityFacilityHandle.del(\'' + rowObject.id + '\')"><i class="fa fa-trash"></i> 删除</a>';
                return html;
            }


            var i = Public.setGrid();

            $("#datatable").jqGrid({
                url: gridQryUrl,
                postData: queryConditions,
                datatype: "json",
                mtype: 'POST',
                autowidth: true,
                height: i.h,
                //altRows: true,
                gridview: true,
                rownumbers: true,
                //multiselect: true,
                // multiboxonly: true,
                colModel: [
                    {name: 'id', index: 'id', hidden: true, sortable: false, frozen: true}, //rownum

                    { name: "name", label: "名称", align: "left", width: '16%'},
                    { name: "tradname", label: "繁体名称", align: "left", width: '16%'},
                    { name: "remark", label: "备注", align: "left", width: '16%'},
                    //{ name: "description", label: "描述", align: "left", width: '16%', sortable: false},
                    //{ name: "sort_num", label: "排序", align: "left", width: '10%'},
                    { name: "opear", label: "操作", align: "left", width: '20%', formatter: operate, title: false, sortable: false}
                ],
                cmTemplate: {
                    // title: false
                },
                width:"100%",
                autowidth:true,
                sortname: "alter_time",
                sortorder: "desc",
                shrinkToFit: true,
                rowNum: 20,
                rowList: [20, 50, 100, 200],
                viewrecords: true,
                forceFit: false,
                pager: '#table_pager',
                //caption: "角色列表",
                jsonReader: {
                    root: "data.list",//返回的数组集合
                    page: "data.paginator.currentPage",//当前页数
                    total: "data.paginator.pages", //总页数
                    records: "data.paginator.totalRows",//总行数
                },
                loadComplete: function () {//如果数据不存在，提示信息
                    var rowNum = $("#datatable").jqGrid('getGridParam', 'records');
                    if (rowNum == 0 || rowNum == null) {
                        if ($("#norecords").html() == null) {
                            $("#datatable").parent().append('<div class="norecords" id="norecords">没有查询记录！</div>');
                        }
                        $("#norecords").show();
                    } else {
                        $("#norecords").hide();
                    }
                },
                loadError: function (xhr, status, error) {
                    if (xhr.status == 403) {
                        parent.layer.msg("无此资源权限，请联系管理员!", {icon: 2});
                    } else if (xhr.status == 500) {
                        parent.layer.msg("加载数据异常!", {icon: 2});
                    } else {
                        parent.layer.msg("加载数据异常!", {icon: 2});
                    }
                },
                ondblClickRow: function (t) {
                    //   SNCreater.view(t);
                }
            })
        },
        reloadData: function (t) {
            $("#datatable").jqGrid("setGridParam", {
                url: gridQryUrl,
                datatype: "json", mtype: 'POST',
                postData: t
            }).trigger("reloadGrid");
        },
        addEvent: function () {
            /*$("#search-btn").on("click", function () {
                var queryData = $("#searchForm").serializeJson();
                CommunityFacility.reloadData(queryData);
            });*/

            $("#clean-btn").on("click", function () {
                $("#keywords").val("");
            });

            $(window).resize(function () {
                Public.resizeGrid()
            })
        },
        selectList:function () {
            var queryData = $("#searchForm").serializeJson();
            Public.autoGrid(jQuery("#datatable"));
            this.reloadData(queryData);
        }



    };


var CommunityFacilityHandle = {

    add: function () {
        openWindow("新增配套", url + '/form', 800, 400, CommunityFacility.reloadData);
    },

    update: function (id) {
        openWindow("修改配套", url + '/form?id=' + id, 800, 400, CommunityFacility.reloadData);
    },

    view: function (id) {
        openDialogView("查看配套", url + '/form?id=' + id, 800, 400);
    },

    del: function (id) {

        layer.confirm('确认删除吗？', {
            btn: ['确认', '取消'] //按钮
        }, function () {
            layer.closeAll();
            Public.ajaxPost(url + "/deleteDictFacility", {
                id: id
            }, function (result) {
                if (result && 200 == result.status) {
                    parent.layer.msg("操作成功!", {icon: 1});
                    CommunityFacility.reloadData();
                } else {
                    parent.layer.msg(result.message, {icon: 2});
                    return;
                }

            });

        });

    }


};

CommunityFacility.init();